//
//  RefreshViewController.m
//  helloword
//
//  Created by chen on 17/3/2.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "RefreshViewController.h"
#import "Commen.h"

@interface RefreshViewController ()

@property (strong,nonatomic) NSMutableArray *Logs;

@end

@implementation RefreshViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.Logs = [[NSMutableArray alloc]init];
    NSDate *date = [[NSDate alloc]init];
    
    [self.Logs addObject:date];
    //初始化下拉刷新控件
    UIRefreshControl *rc = [[UIRefreshControl alloc]init];
    rc.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [rc addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = rc;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)refreshTableView{
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"加载中..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        NSDate *date = [[NSDate alloc]init];
        [self.Logs addObject:date];
    });
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [self.tableView reloadData];
    [self.refreshControl endRefreshing];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_Logs count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [Commen translateFromDate:_Logs[indexPath.row]];
    return cell;
}

//转移至公共类中服用
//-(NSString *)translateFromDate:(NSDate *) date{
//    NSString *dateString = [[NSString alloc]init];
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//    dateString = [formatter stringFromDate:date];
//    return dateString;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
